<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Register extended class used to create base node type</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="domdocument.normalizedocument.html">« DOMDocument::normalizeDocument</a></li>
      <li style="float: right;"><a href="domdocument.relaxngvalidate.html">DOMDocument::relaxNGValidate »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.domdocument.html">DOMDocument</a></li>
    <li>Register extended class used to create base node type</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="domdocument.registernodeclass" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DOMDocument::registerNodeClass</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DOMDocument::registerNodeClass</span> &mdash; <span class="dc-title">Register extended class used to create base node type</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-domdocument.registernodeclass-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DOMDocument::registerNodeClass</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$baseClass</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$extendedClass</code></span>): <span class="type">bool</span></div>


  <p class="para rdfs-comment">
   This method allows you to register your own extended DOM class to be used
   afterward by the PHP DOM extension.
  </p>
  <p class="para">
   This method is not part of the DOM standard.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-domdocument.registernodeclass-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">baseClass</code></dt>

     <dd>

      <p class="para">
       The DOM class that you want to extend. You can find a list of these 
       classes in the <a href="book.dom.html" class="link">chapter introduction</a>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">extendedClass</code></dt>

     <dd>

      <p class="para">
       Your extended class name. If <strong><code>null</code></strong> is provided, any previously 
       registered class extending <code class="parameter">baseClass</code> will
       be removed.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-domdocument.registernodeclass-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-domdocument.registernodeclass-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5403">
    <p><strong>示例 #1 Adding a new method to DOMElement to ease our code</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">myElement&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMElement&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">appendElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">myElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">myDocument&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMDocument&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">setRoot</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">myElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">myDocument</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNodeClass</span><span style="color: #007700">(</span><span style="color: #DD0000">'DOMElement'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'myElement'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;From&nbsp;now&nbsp;on,&nbsp;adding&nbsp;an&nbsp;element&nbsp;to&nbsp;another&nbsp;costs&nbsp;only&nbsp;one&nbsp;method&nbsp;call&nbsp;!&nbsp;<br /></span><span style="color: #0000BB">$root&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setRoot</span><span style="color: #007700">(</span><span style="color: #DD0000">'root'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$child&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$root</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendElement</span><span style="color: #007700">(</span><span style="color: #DD0000">'child'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$child</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAttribute</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;root&gt;&lt;child foo=&quot;bar&quot;/&gt;&lt;/root&gt;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5404">
    <p><strong>示例 #2 Retrieving elements as custom class</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">myElement&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMElement&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__toString</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">nodeValue</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">"&lt;root&gt;&lt;element&gt;&lt;child&gt;text&nbsp;in&nbsp;child&lt;/child&gt;&lt;/element&gt;&lt;/root&gt;"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNodeClass</span><span style="color: #007700">(</span><span style="color: #DD0000">"DOMElement"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"myElement"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$element&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"child"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;And&nbsp;take&nbsp;advantage&nbsp;of&nbsp;the&nbsp;__toString&nbsp;method..<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$element</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(9) &quot;myElement&quot;
text in child
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5405">
    <p><strong>示例 #3 Retrieving owner document</strong></p>
    <div class="example-contents"><p>
     When instantiating a custom <span class="classname"><a href="class.domdocument.html" class="classname">DOMDocument</a></span> the
     <var class="varname">ownerDocument</var> property will refer to the instantiated
     class. However, if all references to that class are removed, it
     will be destroyed and new <span class="classname"><a href="class.domdocument.html" class="classname">DOMDocument</a></span> will be
     created instead. For that reason you might use
     <span class="function"><strong>DOMDocument::registerNodeClass()</strong></span> with
     <span class="classname"><a href="class.domdocument.html" class="classname">DOMDocument</a></span>
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyDOMDocument&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMDocument&nbsp;</span><span style="color: #007700">{<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">MyOtherDOMDocument&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMDocument&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;MyDOMDocument&nbsp;with&nbsp;some&nbsp;XML<br /></span><span style="color: #0000BB">$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyDOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">"&lt;root&gt;&lt;element&gt;&lt;child&gt;text&nbsp;in&nbsp;child&lt;/child&gt;&lt;/element&gt;&lt;/root&gt;"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$child&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"child"</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;current&nbsp;owner&nbsp;of&nbsp;the&nbsp;node&nbsp;is&nbsp;MyDOMDocument<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$child</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ownerDocument</span><span style="color: #007700">));<br /></span><span style="color: #FF8000">//&nbsp;MyDOMDocument&nbsp;is&nbsp;destroyed<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;And&nbsp;new&nbsp;DOMDocument&nbsp;instance&nbsp;is&nbsp;created<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$child</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ownerDocument</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;Import&nbsp;a&nbsp;node&nbsp;from&nbsp;MyDOMDocument<br /></span><span style="color: #0000BB">$newdoc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyOtherDOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$child&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">importNode</span><span style="color: #007700">(</span><span style="color: #0000BB">$child</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Register&nbsp;custom&nbsp;DOMDocument<br /></span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNodeClass</span><span style="color: #007700">(</span><span style="color: #DD0000">"DOMDocument"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"MyOtherDOMDocument"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$child</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ownerDocument</span><span style="color: #007700">));<br />unset(</span><span style="color: #0000BB">$doc</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;New&nbsp;MyOtherDOMDocument&nbsp;is&nbsp;created<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$child</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ownerDocument</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(13) &quot;MyDOMDocument&quot;
string(11) &quot;DOMDocument&quot;
string(18) &quot;MyOtherDOMDocument&quot;
string(18) &quot;MyOtherDOMDocument&quot;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="domdocument.registernodeclass.example.transient">
    <p><strong>示例 #4 Custom objects are transient</strong></p>
    <div class="caution"><strong class="caution">警告</strong>
     <p class="simpara">
      Objects of the registered node classes are transient, i.e. they are
      destroyed when they are no longer referenced from PHP code, and recreated
      when being retrieved again. That implies that custom property values will be
      lost after recreation.
     </p>
    </div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyDOMElement&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">DOMElement<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$myProp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'default&nbsp;value'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$doc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerNodeClass</span><span style="color: #007700">(</span><span style="color: #DD0000">'DOMElement'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'MyDOMElement'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$node&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createElement</span><span style="color: #007700">(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$node</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">myProp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'modified&nbsp;value'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$node</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">childNodes</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">myProp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />unset(</span><span style="color: #0000BB">$node</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$doc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">childNodes</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">myProp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
modified value
default value
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>